package com.sevenyear.excelanalyze.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sevenyear.excelanalyze.entity.ChannelInfiltrationRate;
import com.sevenyear.excelanalyze.entity.param.queryConditionParam.RateQueryCondition;
import com.sevenyear.excelanalyze.listener.excel.ChannelInfiltrationRateListener;
import com.sevenyear.excelanalyze.mapper.ChannelInfiltrationRateMapper;
import com.sevenyear.excelanalyze.service.ChannelInfiltrationRateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sevenyear.excelanalyze.service.PartitionBaseInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * <p>
 * 渠系入渗补给系数 服务实现类
 * </p>
 *
 * @author sevenyear
 * @since 2021-08-20
 */
@Service
public class ChannelInfiltrationRateServiceImpl extends ServiceImpl<ChannelInfiltrationRateMapper, ChannelInfiltrationRate> implements ChannelInfiltrationRateService {

    @Override
    public void insertRainfallInfiltrationRateByExcel(MultipartFile file, ChannelInfiltrationRateService channelInfiltrationRateService, PartitionBaseInfoService partitionBaseInfoService) {
        InputStream in = null;
        try {
            in = file.getInputStream();
            EasyExcel.read(in, ChannelInfiltrationRate.class, new ChannelInfiltrationRateListener(partitionBaseInfoService,channelInfiltrationRateService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void queryPageByCondition(Page<ChannelInfiltrationRate> page, RateQueryCondition rateQueryCondition) {
        QueryWrapper<ChannelInfiltrationRate> wrapper = new QueryWrapper<>();
        if (rateQueryCondition == null) {
            baseMapper.selectPage(page, wrapper);
            return;
        }
        String area = rateQueryCondition.getArea();
        String landform = rateQueryCondition.getLandform();
        String clayThickness = rateQueryCondition.getClayThickness();
        if (!StringUtils.isEmpty(area)) {
            wrapper.like("area", area);
        }
        if (!StringUtils.isEmpty(landform)) {
            wrapper.eq("landform", landform);
        }
        if (!StringUtils.isEmpty(clayThickness)) {
            wrapper.eq("clay_thickness", clayThickness);
        }
        baseMapper.selectPage(page,wrapper);
    }
}
